package duppentity.dto.sys;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import utils.JSONObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangzhi
 * @date 2018/12/7
 * @Desc 数据转换
 */
public class SysDtoUtil {

	public static SysItemDto getSysItemDto(JsonObject data) {
		SysItemDto sysItemDto =new SysItemDto();
		if (data!=null) {
			if (data.has("total")) {
				if (data.get("total").getAsInt() > 0) {
					JsonArray list = data.getAsJsonArray("list");
					JSONObject listOne = JSONObjectUtils.get(list, 0);
					sysItemDto = convertSysItemDto(listOne);
				}
			}
		}
		return sysItemDto;
	}

	public static SysItemDto convertSysItemDto(JSONObject listOne){
		SysItemDto sysItemDto = new SysItemDto();
		sysItemDto.id = listOne.getString("id");
		sysItemDto.parentId = listOne.getLongValue("parentId");
		sysItemDto.level = listOne.getIntValue("level");
		sysItemDto.name = listOne.getString("name");
		sysItemDto.style = listOne.getString("style");
		sysItemDto.sequence = listOne.getInteger("sequence");
		sysItemDto.itemHref = listOne.getString("itemHref");
		sysItemDto.createBy = listOne.getLongValue("createBy");
		sysItemDto.modifyBy = listOne.getLongValue("modifyBy");
		sysItemDto.createTime = listOne.getLong("createTime");
		sysItemDto.modifyTime = listOne.getLong("modifyTime");
		return  sysItemDto;
	}



	public static List<SysItemDto> getSysItemListDto(JsonObject data){
		List<SysItemDto> sysItemDtoList = null;
		if (data!=null) {
			if (data.has("total")) {
				if (data.get("total").getAsInt() > 0) {
					JsonArray list = data.getAsJsonArray("list");
					if (list != null && list.size() > 0) {
						sysItemDtoList = new ArrayList<>();
						for (Integer i=0;i<list.size();i++) {
							JSONObject listOne = JSONObjectUtils.get(list, i);
							SysItemDto adDto = convertSysItemDto(listOne);
							sysItemDtoList.add(adDto);
						}
					}
				}
			}
		}
		return  sysItemDtoList;
	}



	public static List<SysRoleDto> getSysRoleListDto(JsonObject data){
		List<SysRoleDto> sysRoleDtoList = null;
		if (data!=null) {
			if (data.has("total")) {
				if (data.get("total").getAsInt() > 0) {
					JsonArray list = data.getAsJsonArray("list");
					if (list != null && list.size() > 0) {
						sysRoleDtoList = new ArrayList<>();
						for (Integer i=0;i<list.size();i++) {
							JSONObject listOne = JSONObjectUtils.get(list, i);
							SysRoleDto sysRoleDto = convertSysRoleDto(listOne,data);
							sysRoleDtoList.add(sysRoleDto);
						}
					}
				}
			}
		}
		return  sysRoleDtoList;
	}

	public static SysRoleDto getSysRoleDto(JsonObject data) {
		SysRoleDto sysRoleDto =new SysRoleDto();
		if (data!=null) {
			if (data.has("total")) {
				if (data.get("total").getAsInt() > 0) {
					JsonArray list = data.getAsJsonArray("list");
					JSONObject listOne = JSONObjectUtils.get(list, 0);
					sysRoleDto = convertSysRoleDto(listOne,data);
				}
			}
		}
		return sysRoleDto;
	}

	/**
	 *
	 * @param listOne
	 * @param data
	 */
	private static SysRoleDto convertSysRoleDto(JSONObject listOne,JsonObject data){
		SysRoleDto sysRoleDto = new SysRoleDto();
		sysRoleDto.id = listOne.getString("id");
		sysRoleDto.name = listOne.getString("name");
		sysRoleDto.createBy = listOne.getLongValue("createBy");
		sysRoleDto.modifyBy = listOne.getLongValue("modifyBy");

		sysRoleDto.createTime = listOne.getLongValue("createTime");
		sysRoleDto.modifyTime = listOne.getLongValue("modifyTime");
		JSONObject sysAdmin = JSONObjectUtils.get(data, "sys_admin");
		if (sysRoleDto.createBy > 0) {
			sysRoleDto.createName = JSONObjectUtils.getString(sysAdmin, sysRoleDto.createBy.toString(), "name");
		}
		if (sysRoleDto.modifyBy > 0) {
			sysRoleDto.modifyName = JSONObjectUtils.getString(sysAdmin, sysRoleDto.modifyBy.toString(), "name");
		}
		sysRoleDto.userNumber = listOne.getJSONArray("sysAdminRoleIds").size();
		JSONArray jsonArray =listOne.getJSONArray("sysRoleItemIds");
		if (jsonArray.size()>0) {
			JSONObject sysRoleItem = JSONObjectUtils.get(data, "sys_role_item");
			List<Long> result = new ArrayList<>();
			for (Integer i=0;i<jsonArray.size();i++){
				Long id = jsonArray.getLong(i);
				Long itemId =JSONObjectUtils.getLong(sysRoleItem, id.toString(), "itemId");
				result.add(itemId);
			}
			sysRoleDto.roleItemIds =result;
		}

		return sysRoleDto;
	}



}
